package source
{
	import com.lipi.excel.Excel;


	/**
	 *	配置表结构强类型
	 * @author Roland
	 *
	 */
	public class JsonObj
	{
		/**
		 *	表格数据的索引行,从0开始;
		 */
		public static const INDEX_ROW:int = 1;

		/**
		 *	 表格数据的描述行,从0开始
		 */
		public static const IGNORE_ROW:int = 0;

		/**
		 * 素材配置文件
		 */
		public static const CONFIG_MATS:String = "Material";

		/**
		 *	关卡配置
		 */
		public static const CONFIG_SCENE:String = "scene";

		public function JsonObj()
		{
		}

		/**
		 *	json数据表名
		 */
		public var name:String;

		/**
		 *	Excel格式的数据
		 */
		public var excelData:Excel;

		/**
		 *	 object形式的数据
		 */
		public var objectData:Object;

		/**
		 *	 array形式的数据
		 */
		public function get arrayData():Array
		{
			return objectData as Array;
		}

		/**
		 *	此配置文件是否是Materail素材配置表
		 * @return
		 *
		 */
		public function isMaterialConfig():Boolean
		{
			return name == CONFIG_MATS;
		}

		/**
		 *	填充数据
		 * @param name 表名
		 * @param excel 表格数据
		 * @return
		 *
		 */
		public static function parseData(filename:String, excel:Excel):JsonObj
		{
			var $obj:JsonObj = new JsonObj();
			$obj.name = filename;
			$obj.excelData = excel;
			$obj.objectData = excelToObject(filename, excel);
			return $obj;
		}

		/**
		 *	 用json字符串填充数据
		 * @param filename 表名
		 * @param jsonStr json字符串数据
		 * @return
		 *
		 */
		public static function parseJsonData(filename:String, jsonStr:String):JsonObj
		{
			var $obj:JsonObj = new JsonObj();
			$obj.name = filename;
			$obj.objectData = JSON.parse(jsonStr);//JSON.decode(jsonStr);
			return $obj;
		}

		/**
		 *	把excel表格转化为Object索引数据
		 * @param filename
		 * @param excel
		 * @return
		 *
		 */
		private static function excelToObject(filename:String, excel:Excel):Object
		{
			var $newExcel:Excel = new Excel(excel.getResult());
			var $sheetArray:Array = $newExcel.getSheetArray();
			var $indexArray:Array = $sheetArray[INDEX_ROW];
			var $dataArray:Array = [];
			var $itemData:Object;
			for (var rowNum:int = 0; rowNum < $sheetArray.length; rowNum++)
			{
				if (rowNum == INDEX_ROW)
					continue; //跳过索引行
				if (rowNum == IGNORE_ROW)
					continue; //跳过描述行
				var array:Array = $sheetArray[rowNum];
				if (array != null)
				{
					var rowdata:Array = array;
					$itemData = new Object();
					var tempList:Array;
					var num:Array;
					var count:Number = 0;
					var i:int;
					var manner:Object;
					for (var j:int = 0; j < rowdata.length; j++)
					{
						if (rowdata[j] != null)
						{
							break;
						}
						else
						{
							count++;
						}
					}
					if (0 <= count && count < rowdata.length)
					{
						if (filename == CONFIG_MATS) //处理Materail
						{
							var $hasDataflag:Boolean = false;
							for (var m:int = 0; m < rowdata.length; m++)
							{
								if ($indexArray[m] == "objectId")
								{
									$itemData["serverId"] = rowdata[m];
									$hasDataflag = true;
								}
								if ($indexArray[m] == "subclass")
								{
									$itemData["subclass"] = rowdata[m] ? rowdata[m] : "";
									$hasDataflag = true;
								}
								if ($indexArray[m] == "fileName")
								{
									$itemData["swfurl"] = rowdata[m];
									$itemData["fileName"] = rowdata[m];
									$hasDataflag = true;
								}
							}
							if ($hasDataflag)
							{
								$dataArray.push($itemData);
							}
						}
						else //默认方式填充
						{
							for (var k:int = 0; k < $indexArray.length; k++)
							{
								if ($indexArray[k] != null)
								{
									$itemData[$indexArray[k]] = formatData(rowdata[k]);
								}
							}
							$dataArray.push($itemData);
						} //end if
					}
				}
			}
			return $dataArray;
		}

		/**
		 *	 格式化字符串数据
		 * @param str
		 *
		 */
		private static function formatData(str:String):*
		{
			if (str == null)
			{
				return "";
			}
			else if (Number(str).toString() == str) //如果是纯数字
			{
				return Number(str);
			}
			else
			{
				return str;
			}
		}
	}
}
